package com.kamm.springbootflower.action;

import com.kamm.springbootflower.domain.Flower;
import com.kamm.springbootflower.domain.FlowerTb;
import com.kamm.springbootflower.domain.Flowertype;
import com.kamm.springbootflower.service.FlowerService;
import com.kamm.springbootflower.service.FlowerTbService;
import com.kamm.springbootflower.service.FlowertypeService;
import com.kamm.springbootflower.utils.FileTools;
import com.kamm.springbootflower.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("/flower")
public class flowerController {

    @Resource
    FlowerService flowerService;

    @Resource
    FlowertypeService flowertypeService;

    @Resource
    FlowerTbService flowerTbService;

    @Resource
    FileTools fileTools;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(Flower flower, @RequestParam(value = "page",required=false) Integer pageNum,@RequestParam(value = "limit",required = false) Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        int totalRows = flowerService.count(flower);
        List<Flower> list= flowerService.findAll(flower, pageNum,pageSize);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", totalRows);
        map.put("data", list);
        return map;
    }
    @RequestMapping("/addOrupdate")
    @ResponseBody
    public Message addOrUpdate(Flower flower, @RequestParam(value = "id",required = false) Integer id, String f_image,Flowertype flowertype) throws IOException {
        Message msg = new Message();
        FlowerTb flowerTb=new FlowerTb();
        flowerTb.setFtId(flowertype.getFtId());
        flowerTb.setfId(flower.getfId());
        if (flower.getfId()!=null&&!"".equals(flower.getfId())) {
            flower.setfId(id);
            flowerService.update(flowerTb);
            int rows = flowerService.updateByPrimaryKey(flower);
            if(f_image!=null&&!f_image.equals("")) {
                Flower flower1=flowerService.selectByPrimaryKey(flower.getfId());
                flower.setfId(flower1.getfId());
                flowerService.update(flowerTb);
                flowerService.updateByPrimaryKey(flower);
                if(flower.getFileone().getOriginalFilename()!=null&&!"".equals(flower.getFileone().getOriginalFilename())) {
                    String path= fileTools.saveFile("flower",flower.getFileone());
                    flower.setfId(flower1.getfId());
                    flower.setfImage(path);
                    flowerService.update(flowerTb);
                    flowerService.updateByPrimaryKey(flower);
                    if (rows > 0) {
                        msg.setMsg("操作成功!");
                        msg.setNum(1);
                    } else {
                        msg.setNum(0);
                        msg.setMsg("操作失败");
                    }
                }
            }
        } else {
            if(!StringUtils.isEmpty(flower.getFileone().getOriginalFilename())){
               String path= fileTools.saveFile("flower",flower.getFileone());
                flower.setfImage(path);
                flower.setfId(flower.getfId());
            }
            int rows= flowerService.insert(flower);
            flowerTb.setFtId(flowertype.getFtId());
            flowerTb.setfId(flower.getfId());
            flowerService.addType(flowerTb);
            if(rows>0){
                msg.setMsg("操作成功!");
                msg.setNum(1);

            }else{
                msg.setNum(0);
                msg.setMsg("操作失败");
            }
        }
        return msg;
    }
    @RequestMapping("/findById")
    @ResponseBody
    public  Flower findById(Integer fId){
        Flower flower=flowerService.findById(fId);
       // List<Flowertype> flowertypeList=flower.getFlowertypeList();
        return flower;
    }
    @RequestMapping("/del")
    @ResponseBody
    public Message delete(Flower flower,FlowerTb flowerTb){
        Message msg = new Message();
        if(flower.getfId()!=null&&!"".equals(flower.getfId())){
            int rows=flowerService.updateByPrimaryKey(flower);
            flowerService.deleteById(flower.getfId());
            flowerService.deleteByPrimaryKey(flower.getfId());
            if(rows>0){
                msg.setMsg("成功");
                msg.setNum(1);
            }else {
                msg.setMsg("失败");
                msg.setNum(0);
            }
        }else {
            msg.setMsg("错误!");
            msg.setNum(3);
        }
        return msg;

    }
    @RequestMapping("/delAll")
    @ResponseBody
    public Message deleteAll(String[] ids){
        Message msg = new Message();
        flowerService.delete(ids);
        if(flowerService.deleteAll(ids)==0){
            msg.setMsg("失败");
            msg.setNum(0);
        }else {
            msg.setMsg("成功");
            msg.setNum(1);
        }
        return msg;
    }
}

